package com
{
	import com.mathMl.MathPanel;
	import com.mathMl.MathPanelEvent;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Mohan Patil
	 */
	public class AdvDropDownItem extends MovieClip
	{
		private var _id:Number;
		private var _type:String;
		private var _data:String;
		private var _addedToItemList:Boolean = false;
		private var _treatAsSelectedItem:Boolean = false;
		private var _dropDownWidth:Number;
		private var _fontFamily:String;
		private var _loaderInfoObj:LoaderInfo;
		private var _fontSize:Number;
		private var _textFormat:TextFormat;
		
		public function AdvDropDownItem(itemID:Number = -1, itemType:String = "", itemData:String = "")
		{
			_id = itemID;
			_type = itemType;
			_data = itemData;
			
			//mcBg.width = _itemWidth;
			//updateItem();
		}
		
		//public function renderItem(fontFamily:String = "", fontSize:Number = 0 , loaderInfoObj:LoaderInfo = null, dropDownWidth:Number = 0):void
		public function renderItem(textFormat:TextFormat, dropDownWidth:Number = 0):void
		{
			var i;
			//_fontFamily = fontFamily;
			//_fontSize = fontSize;
			//_loaderInfoObj = loaderInfoObj
			//_fontSize = fontSize;
			_textFormat = textFormat;
			_dropDownWidth = dropDownWidth;
			txtContent.htmlText = "";
			//trace("mcContent.numChildren (Before) = " + mcContent.numChildren);
			i = 0;
			while (i < mcContent.numChildren)
			{	
				mcContent.removeChild(mcContent.getChildAt(i));
			}
			//trace("mcContent.numChildren (After) = " + mcContent.numChildren);
			switch(_type)
			{
				case "text":
				{
					//trace("Type : Text");
					//var txtFormat:TextFormat = new TextFormat();
					//txtFormat.size = _fontSize;
					//txtFormat.font = _fontFamily;
					//trace(data);
					txtContent.multiline = true;
					txtContent.htmlText = _data;
					txtContent.wordWrap = true;
					txtContent.setTextFormat(_textFormat);
					txtContent.embedFonts = true;
					txtContent.selectable = false;
					txtContent.antiAliasType = AntiAliasType.ADVANCED;
					txtContent.autoSize = TextFieldAutoSize.LEFT;
					txtContent.width = _dropDownWidth - 10;
					mcBg.width = _dropDownWidth;
					mcBg.height = txtContent.textHeight;
					mcPatch.width = mcBg.width;
					mcPatch.height = mcBg.height;
					_addedToItemList = true;
					dispatchEvent(new AdvDropDownEvent(AdvDropDownEvent.ITEM_ADDED, false, false));
					break;
				}
				case "image":
				{
					//trace("Type : Image");
					var imgLoader:Loader = new Loader;
					var urlReq:URLRequest = new URLRequest(String(_data));
					imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
					imgLoader.load(urlReq);
					imgLoader.name = "mcImage_" + _id;
					mcContent.addChild(imgLoader);
					break;
				}
				case "math":
				{
					//trace("Type : MathMl");
					var panel1:MathPanel = new MathPanel();
					panel1.addEventListener(MathPanelEvent.ON_FORMULA_ADDED, formulaAdded);
					panel1.name = "mcMathPanel_" + _id;
					mcContent.addChild(panel1);
					//panel1.drawFormula(_data, _fontSize + 4);
					panel1.drawFormula(_data, textFormat.size + 4);
					break;
				}
				default:
				{
					break;
				}
			}
			
		}
		
		private function onImageLoaded(e:Event):void 
		{
			mcBg.width = _dropDownWidth;
			mcBg.height = mcContent.height + 2;
			mcPatch.width = mcBg.width;
			mcPatch.height = mcBg.height;
			_addedToItemList = true;
			dispatchEvent(new AdvDropDownEvent(AdvDropDownEvent.ITEM_ADDED, false, false));
		}
		
		private function formulaAdded(e:MathPanelEvent):void
		{
			//trace("FormulaAdded Called");
			var tmpPanel:MathPanel = MathPanel(e.target);
			//trace("Formula Added Called For " + tmpPanel.name);
			tmpPanel.x = 2;
			tmpPanel.y = tmpPanel.y + tmpPanel.height / 2;
			//trace("tmpPanel.height = " + tmpPanel.height);
			mcBg.width = _dropDownWidth;
			mcBg.height = mcContent.height + 2;
			
			mcPatch.width = mcBg.width;
			mcPatch.height = mcBg.height;
			_addedToItemList = true;
			dispatchEvent(new AdvDropDownEvent(AdvDropDownEvent.ITEM_ADDED, false, false));
			//mcBg.width = mcContent.width + 10;
			//mcBg.height = mcContent.height + 10;
		}
		
		public function get id():Number 
		{
			return _id;
		}
		
		public function set id(value:Number):void 
		{
			_id = value;
		}
		
		public function get type():String 
		{
			return _type;
		}
		
		public function set type(value:String):void 
		{
			_type = value;
		}
		
		public function get data():String 
		{
			return _data;
		}
		
		public function set data(value:String):void 
		{
			_data = value;
		}
		
		public function get addedToItemList():Boolean 
		{
			return _addedToItemList;
		}
		
		public function set addedToItemList(value:Boolean):void 
		{
			_addedToItemList = value;
		}
		
	}
}